package cn.lzj.service.User.impl.user;


import cn.lzj.Utils.TimeUtils;
import cn.lzj.mapper.*;
import cn.lzj.mapper.UserMapper;
import cn.lzj.pojo.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class UserServiceImpl implements cn.lzj.service.User.UserService {

    @Resource
    private CollectMapper collectMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private StoreInfoMapper storeInfoMapper;

    @Resource
    private HistoryRecordMapper historyRecordMapper;

    @Resource
    private EvaluateMapper evaluateMapper;

    @Resource
    private PayTableMapper payTableMapper;

    //  取消订单
    //  实现  ——zsy

    @Transactional
    @Override
    public boolean CancelOrder(PayTable payTable) {

        List<Order> orderList = orderMapper.selectOrderByPayId(payTable.getPayId());
        for(int i = 0;i<orderList.size();i++){
            Order order = orderList.get(i);
            orderMapper.deleteOrder(order);
            ProductBase product = productMapper.getProductById(order.getProductBase().productId);

            //取消订单  库存+1  销量-1
            product.setProductCount(product.getProductCount()+1);
            product.setSellNum(product.getSellNum()-1);

            productMapper.updateProduct(product);
        }
        int count = payTableMapper.deletePayTableItem(payTable);
        return count == 1;
    }




    //  放置
    @Override
    public boolean PayOrder(PayTable pay) {
        int count = payTableMapper.updatePayTable(pay);
        return count == 1;
    }

    //  用户评价
    @Override
    public boolean EvaluateProduct(Evaluate evaluate) {
        int addEvaluate = evaluateMapper.addEvaluate(evaluate);
        //  添加之后如何判断是否添加成功
        if(addEvaluate != 0){
            return true;
        }
        return false;
    }

//
//    @Override
//    public List<HistoryRecord> MyHistoryRecord(User user) {
//        List<HistoryRecord> historyRecords = historyRecordMapper.MyHistoryRecord(user.getUserId());
//        return historyRecords;
//    }

    //  商品信息表查价格
    //  用商品id找productBase和Evaluate信息
    @Override
    public Map<ProductBase, List<Evaluate>> selectDetailProduct(Integer productId){
        //  获取productBase记录
        ProductBase productBases = productMapper.getProductById(productId);
        //  获取evaluate记录
        List<Evaluate> evaluates = evaluateMapper.selectEvaluateByProductId(productId);
        //  定义集合    一个productBase对应一个评价
        Map<ProductBase, List<Evaluate>> map = new HashMap<>();
        //  存入map
        map.put(productBases, evaluates);
        return map;
    }

    @Transactional
    @Override
    //加时间参数
    public boolean Shopping(PayTable payTable,Order order) {
        boolean shopResult = payTableMapper.generateOrderList(payTable);

        PayTable payTableHavaPayNo = payTableMapper.getPayTableByPayno(payTable);
        order.setPayTable(payTableHavaPayNo);
        orderMapper.addOrderToT_order(order);

        if(shopResult){
            return true;
        }
        return false;
    }

    //  模糊查询
    @Override
    public List<Collect> selectCollect(User user, String collectSearch) {
        List<Collect> collectList = collectMapper.selectCollect(user, collectSearch);
        return collectList;
    }

    //  模糊查询
    @Override
    public List<PayTable> selectOrder(Integer userId, String orderSearch) {
        List<PayTable> payTableList = payTableMapper.selectOrderWithLike(userId, orderSearch);
        for(int i = 0;i<payTableList.size();i++){
            PayTable payTable = payTableList.get(i);
            List<Order> orderList = orderMapper.selectOrderByPayId(payTable.getPayId());

            //这个还需要修改
            payTable.setOrderList(orderList);
        }
        return payTableList;
    }

    //  模糊查询
    @Override
    public List<HistoryRecord> selectHistory(Integer userId, String history) {
        List<HistoryRecord> historyRecords = historyRecordMapper.MyHistoryRecord(userId,history);
        return historyRecords;
    }

    @Override
    public boolean joinLW(String proName, String userName, String addr, String phone) {
        User user_temp = userMapper.selectByPhone(phone);
        if(user_temp == null){
            return false;
        }
        user_temp.setUserType("business");


        Integer userId = user_temp.getUserId();
        int joinLWResult = storeInfoMapper.joinLWShop(proName, addr, phone,userId);
        if(joinLWResult != 0){
            return true;
        }
        return false;
    }

    //  从哪里给collect表传入日期呢
    @Override
    public boolean createCollect(Integer userId, Integer proId) {

        Date date = new Date();
        int collectResult = collectMapper.createCollect(userId, proId,date);
        if(collectResult != 0){
            return true;
        }
        return false;
    }

    //根据id查找用户
    @Override
    public User getUserById(Integer userid) {
        return userMapper.getUserById(userid);
    }

    @Override
    public PayTable getPayTableItemById(Integer payId) {
        PayTable payTableItem = payTableMapper.getPayTableItemById(payId);
        return payTableItem;
    }

    @Override
    public User getUserByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }

    @Override
    public int addHistory(User user, ProductBase product) {
        Date date = new Date();
        long time = date.getTime();
        String recordDate = TimeUtils.TimeMillisContactToString(time);

        return historyRecordMapper.insertHistory(user, product,recordDate);
    }
}
